﻿using System;
using System.Xml.Linq;
using IDeal.Models;
using IDeal.Services;

namespace IDeal.Serialization
{
    internal class StatusResponseXmlReader : XmlMessageReader<StatusResponse>
    {
        public StatusResponseXmlReader(IIDealServices iDealServices)
            : base(iDealServices)
        {
        }

        protected override void ParseRootElement(StatusResponse instance, XElement rootElement)
        {
            var transactionElement = rootElement.Element(Ns + "Transaction");
            var signatureElement = rootElement.Element(Ns + "Signature");

            instance.AcquirerId = rootElement.Element(Ns + "Acquirer").Element(Ns + "acquirerID").Value;
            instance.TransactionId = transactionElement.Element(Ns + "transactionID").Value;
            instance.Status = (TransactionStatus)Enum.Parse(typeof(TransactionStatus), transactionElement.Element(Ns + "status").Value);

            if (instance.Status == TransactionStatus.Success)
            {
                instance.ConsumerName = transactionElement.Element(Ns + "consumerName").Value;
                instance.ConsumerAccountNumber = transactionElement.Element(Ns + "consumerAccountNumber").Value;
                instance.ConsumerCity = transactionElement.Element(Ns + "consumerCity").Value;
            }

            instance.Signature = new Signature
            {
                Token = signatureElement.Element(Ns + "signatureValue").Value,
                Fingerprint = signatureElement.Element(Ns + "fingerprint").Value
            };
        }
    }
}
